﻿#include "lua_obj.h"

static int lua_lv_img_set_src(lua_State *L)
{
    LUA_USER_PRINT(__func__);
    lv_obj_t *a = *(lv_obj_t **)luaL_checkudata(L, 1, META_IMG);
    size_t l;
    const char *s = luaL_checklstring(L, 2, &l);
    lv_img_set_src(a, s);
    return 0;
}

static int lua_lv_img_set_offset_x(lua_State *L)
{
    LUA_USER_PRINT(__func__);
    lv_obj_t *a = *(lv_obj_t **)luaL_checkudata(L, 1, META_IMG);

    return 0;
}

static int lua_lv_img_set_offset_y(lua_State *L)
{
    LUA_USER_PRINT(__func__);
    lv_obj_t *a = *(lv_obj_t **)luaL_checkudata(L, 1, META_IMG);

    return 0;
}

static int lua_lv_img_set_angle(lua_State *L)
{
    LUA_USER_PRINT(__func__);
    lv_obj_t *a = *(lv_obj_t **)luaL_checkudata(L, 1, META_IMG);
    int16_t angle = luaL_checkinteger(L, 2);
    lv_img_set_angle(a, angle);

    return 0;
}

static int lua_lv_img_set_pivot(lua_State *L)
{
    LUA_USER_PRINT(__func__);
    lv_obj_t *a = *(lv_obj_t **)luaL_checkudata(L, 1, META_IMG);

    return 0;
}

static int lua_lv_img_set_zoom(lua_State *L)
{
    LUA_USER_PRINT(__func__);
    lv_obj_t *a = *(lv_obj_t **)luaL_checkudata(L, 1, META_IMG);
    uint16_t zoom = luaL_checkinteger(L, 2);
    lv_img_set_zoom(a, zoom);

    return 0;
}

static int lua_lv_img_set_antialias(lua_State *L)
{
    LUA_USER_PRINT(__func__);
    lv_obj_t *a = *(lv_obj_t **)luaL_checkudata(L, 1, META_IMG);

    return 0;
}

static int lua_lv_img_set_size_mode(lua_State *L)
{
    LUA_USER_PRINT(__func__);
    lv_obj_t *a = *(lv_obj_t **)luaL_checkudata(L, 1, META_IMG);

    return 0;
}

static int lua_lv_img_get_src(lua_State *L)
{
    LUA_USER_PRINT(__func__);
    lv_obj_t *a = *(lv_obj_t **)luaL_checkudata(L, 1, META_IMG);

    return 0;
}

static int lua_lv_img_get_offset_x(lua_State *L)
{
    LUA_USER_PRINT(__func__);
    lv_obj_t *a = *(lv_obj_t **)luaL_checkudata(L, 1, META_IMG);

    return 0;
}

static int lua_lv_img_get_offset_y(lua_State *L)
{
    LUA_USER_PRINT(__func__);
    lv_obj_t *a = *(lv_obj_t **)luaL_checkudata(L, 1, META_IMG);

    return 0;
}

static int lua_lv_img_get_angle(lua_State *L)
{
    LUA_USER_PRINT(__func__);
    lv_obj_t *a = *(lv_obj_t **)luaL_checkudata(L, 1, META_IMG);
    uint16_t res = lv_img_get_angle(a);
    lua_pushinteger(L, res);

    return 1;
}

static int lua_lv_img_get_pivot(lua_State *L)
{
    LUA_USER_PRINT(__func__);
    lv_obj_t *a = *(lv_obj_t **)luaL_checkudata(L, 1, META_IMG);


    return 0;
}

static int lua_lv_img_get_zoom(lua_State *L)
{
    LUA_USER_PRINT(__func__);
    lv_obj_t *a = *(lv_obj_t **)luaL_checkudata(L, 1, META_IMG);
    uint16_t res = lv_img_get_zoom(a);
    lua_pushinteger(L, res);

    return 1;
}

static int lua_lv_img_get_antialias(lua_State *L)
{
    LUA_USER_PRINT(__func__);
    lv_obj_t *a = *(lv_obj_t **)luaL_checkudata(L, 1, META_IMG);

    return 0;
}

static int lua_lv_img_get_size_mode(lua_State *L)
{
    LUA_USER_PRINT(__func__);
    lv_obj_t *a = *(lv_obj_t **)luaL_checkudata(L, 1, META_IMG);

    return 0;
}

static int lua_auto_gc(lua_State *L)
{
    lv_obj_t *lv_obj = *(lv_obj_t **)luaL_checkudata(L, 1, META_IMG);
    luaL_argcheck(L, lv_obj != NULL, 1, "invalid user data");
    if (lv_obj)
    {
        lv_obj_del(lv_obj);
    }
    return 0;
}
static const struct luaL_Reg arraylib_m[] = {
    {"set_src", lua_lv_img_set_src},
    {"set_offset_x", lua_lv_img_set_offset_x},
    {"set_offset_y", lua_lv_img_set_offset_y},
    {"set_angle", lua_lv_img_set_angle},
    {"set_pivot", lua_lv_img_set_pivot},
    {"set_zoom", lua_lv_img_set_zoom},
    {"set_antialias", lua_lv_img_set_antialias},
    {"set_size_mode", lua_lv_img_set_size_mode},
    {"get_src", lua_lv_img_get_src},
    {"get_offset_x", lua_lv_img_get_offset_x},
    {"get_offset_y", lua_lv_img_get_offset_y},
    {"get_angle", lua_lv_img_get_angle},
    {"get_pivot", lua_lv_img_get_pivot},
    {"get_zoom", lua_lv_img_get_zoom},
    {"get_antialias", lua_lv_img_get_antialias},
    {"get_size_mode", lua_lv_img_get_size_mode},
    LUA_LV_OBJ_RLT,
    {NULL, NULL}};

static int help(lua_State *L)
{
    uint16_t n = sizeof(arraylib_m) / sizeof(struct luaL_Reg);
    for (int i = 0; i < n - 1; i++)
    {
        printf("%s\n", arraylib_m[i].name);
    }
    return 1;
}

static const struct luaL_Reg arraylib_f[] = {
    {"help", help},
    {NULL, NULL}};

LUALIB_API int lua_lv_img_funinit(lua_State *L)
{
    luaL_newlib(L, arraylib_f);
    luaL_newmetatable(L, META_IMG);  /* create metatable for file handles */
    lua_pushvalue(L, -1);            /* push metatable */
    lua_setfield(L, -2, "__index");  /* metatable.__index = metatable */
    luaL_setfuncs(L, arraylib_m, 0); /* add file methods to new metatable */
    lua_pop(L, 1);                   /* pop new metatable */

    return 1;
}
